<?php

namespace HugCode\WeChat\Miniprogram;

use HugCode\WeChat\Basics\BasicWeChat;
use HugCode\WeChat\Basics\HttpRequest;

class Login extends BasicWeChat
{

    const JS_CODE2SESSION = 'sns/jscode2session'; // 小程序通过code码登录

    /**
     * @Desc 小程序通过code码登录
     * @param string $code
     * @return mixed|string
     * @throws \HugCode\WeChat\Basics\Exception\MessageException
     * @author yashuai 1762910894@qq.com
     */
    public function jscode2session(string $code)
    {
        $data = [
            'js_code'    => $code,
            'appid'      => $this->config->get('appid'),
            'secret'     => $this->config->get('appsecret'),
            'grant_type' => 'authorization_code',
        ];
        return HttpRequest::instance()->get(self::API_BASE_URL_PREFIX . self::JS_CODE2SESSION, $data)->toArray();
    }

}
